<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController as BaseController;

class AdminController extends BaseController
{

    /**
     * 是否有新增功能
     * @var bool
     */
    public bool $create = true;
    /**
     * 新增功能的权限名称
     * @var string
     */
    public string $create_permission_name = '';
    /**
     * 是否有编辑功能
     * @var bool
     */
    public bool $edit = true;
    /**
     * 编辑功能的权限名称
     * @var string
     */
    public string $edit_permission_name = '';
    /**
     * 是否有删除功能
     * @var bool
     */
    public bool $delete = true;
    /**
     * 删除功能的权限名称
     * @var string
     */
    public string $delete_permission_name = '';


    /**
     *  开启字段选择器功能
     * @var bool
     */
    public bool $show_column_selector = true;


    /**
     * 正常模式列隐藏的字段
     * @var array
     */
    public array $normal_hide_columns = [];


    /**
     *  禁用分页
     * @var bool
     */
    public bool $disable_pagination = false;


    /**
     *  禁用行操作
     * @var bool
     */
    public bool $disable_actions = false;


    //隐藏批量操作按钮
    public bool $disable_row_selector = true;


    //表格的公共操作
    public function gridCommon(Grid $grid)
    {
        //添加
        if (!$this->create || ($this->create && Admin::user()->cannot($this->create_permission_name))) {
            $grid->disableCreateButton();
        }
        //修改
        if ($this->edit && Admin::user()->can($this->edit_permission_name) && !$this->isTrashed()) {
            $grid->showQuickEditButton();
        }
        //删除
        if (!$this->delete || ($this->delete && Admin::user()->cannot($this->delete_permission_name)) || $this->isTrashed()) {
            $grid->disableDeleteButton();
        }

        if ($this->show_column_selector) {
            $grid->hideColumns($this->normal_hide_columns);
        }

        //关闭分页
        if ($this->disable_pagination) {
            $grid->disablePagination();
        }

        //禁用行操作
        if ($this->disable_actions) {
            $grid->disableActions();
        }
        if ($this->disable_row_selector) {
            $grid->disableRowSelector();
        }
    }


    //判断当前是否是回收站，如果是回收站需要关闭操作相关的功能
    public function isTrashed(): bool
    {
        return request('_scope_') == 'trashed';
    }

}
